package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.UserRelationMapper;
import com.ruoyi.system.domain.UserRelation;
import com.ruoyi.system.service.IUserRelationService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 用户关联Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class UserRelationServiceImpl implements IUserRelationService 
{
    @Autowired
    private UserRelationMapper userRelationMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询用户关联
     * 
     * @param id 用户关联主键
     * @return 用户关联
     */
    @Override
    public UserRelation selectUserRelationById(Long id)
    {
        return userRelationMapper.selectUserRelationById(id);
    }

    /**
     * 查询用户关联列表
     * 
     * @param userRelation 用户关联
     * @return 用户关联
     */
    @Override
    public List<UserRelation> selectUserRelationList(UserRelation userRelation)
    {
        return userRelationMapper.selectUserRelationList(userRelation);
    }

    /**
     * 新增用户关联
     * 
     * @param userRelation 用户关联
     * @return 结果
     */
    @Override
    public int insertUserRelation(UserRelation userRelation)
    {
        // 获取老人和家属的用户信息
        SysUser elderUser = userService.selectUserById(userRelation.getElderUserId());
        SysUser familyUser = userService.selectUserById(userRelation.getFamilyUserId());
        
        // 设置姓名
        userRelation.setElderName(elderUser.getNickName());
        userRelation.setFamilyName(familyUser.getNickName());
        
        return userRelationMapper.insertUserRelation(userRelation);
    }

    /**
     * 修改用户关联
     * 
     * @param userRelation 用户关联
     * @return 结果
     */
    @Override
    public int updateUserRelation(UserRelation userRelation)
    {
        // 获取老人和家属的用户信息
        SysUser elderUser = userService.selectUserById(userRelation.getElderUserId());
        SysUser familyUser = userService.selectUserById(userRelation.getFamilyUserId());
        
        // 设置姓名
        userRelation.setElderName(elderUser.getNickName());
        userRelation.setFamilyName(familyUser.getNickName());
        
        return userRelationMapper.updateUserRelation(userRelation);
    }

    /**
     * 批量删除用户关联
     * 
     * @param ids 需要删除的用户关联主键
     * @return 结果
     */
    @Override
    public int deleteUserRelationByIds(String[] ids)
    {
        return userRelationMapper.deleteUserRelationByIds(ids);
    }

    /**
     * 删除用户关联信息
     * 
     * @param id 用户关联主键
     * @return 结果
     */
    @Override
    public int deleteUserRelationById(Long id)
    {
        return userRelationMapper.deleteUserRelationById(id);
    }

    /**
     * 根据老人用户ID查询关联的家属列表
     * 
     * @param elderUserId 老人用户ID
     * @return 用户关联列表
     */
    @Override
    public List<UserRelation> selectUserRelationByElderUserId(Long elderUserId)
    {
        return userRelationMapper.selectUserRelationByElderUserId(elderUserId);
    }

    /**
     * 根据家属用户ID查询关联的老人列表
     * 
     * @param familyUserId 家属用户ID
     * @return 用户关联列表
     */
    @Override
    public List<UserRelation> selectUserRelationByFamilyUserId(Long familyUserId)
    {
        return userRelationMapper.selectUserRelationByFamilyUserId(familyUserId);
    }

    @Override
    public List<SysUser> selectFamilyListByElderId(Long elderUserId)
    {
        return userRelationMapper.selectFamilyListByElderId(elderUserId);
    }

    @Override
    public List<SysUser> selectElderListByFamilyId(Long familyId)
    {
        return userRelationMapper.selectElderListByFamilyId(familyId);
    }
} 